package main;

import java.io.FileNotFoundException;

import parse.*;
import Const.Constant;
import ExecuteEngine.*;
import SystemManagement.*;

public class test2 {
	public static void main(String[] args) throws FileNotFoundException {
		SM_Manager sm = new SM_Manager();
		
//		sm.CreateDB("miniDB");
		sm.OpenDb("miniDB");
	
		ExecuteEngine engine = new ExecuteEngine(sm);
				
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\c5.tig", sm);
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\c6.tig", sm);
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\i8.tig", sm);
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\i9.tig", sm);
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\i10.tig", sm);
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\i11.tig", sm);
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\i12.tig", sm);
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\i13.tig", sm);
//		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\i18.tig", sm);
		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\i20.tig", sm);
		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\q3.tig", sm);
		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\q8.tig", sm);
		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\q1.tig", sm);
		new test1().Explain("E:\\Science\\CS\\DB\\Project(DB)\\Project\\MiniDB\\src\\main\\q6.tig", sm);
	} 
	
	public void Explain(String filename, SM_Manager sm){
		Parse p=new Parse(filename);
		
		if(p.absyn instanceof CreateTable){
			((CreateTable)p.absyn).CrtTable(sm);
		}
		else
		if(p.absyn instanceof Insert){
			((Insert)p.absyn).insertTuple(sm);
		}
		else
		if(p.absyn instanceof SFW){
			ExecuteEngine engine = new ExecuteEngine(sm);
			Operation query = engine.PQPlan(((SFW)p.absyn).CrtAgbPlan(sm));
			
			query.Open();
			
			boolean mark = true;
			while(query.hasNext()){
				Tuple t = query.GetNext();

				if(mark){
					for(int i=0; i<t.tuple.size(); i++){
						Element e = t.tuple.get(i);
						System.out.print(e.attrName.toUpperCase());
					}
					mark = false;
					System.out.println();
				}
				
				for(int i=0; i<t.tuple.size(); i++){
					DataItem value = t.tuple.get(i).value;
					
					if(value instanceof IntegerItem){
						StringBuffer s = new StringBuffer(new String().valueOf(((IntegerItem)value).value));
						s.setLength(Constant.MAXNAME);
						for(int j=0; j<Constant.MAXNAME; j++){
							if(s.charAt(j) != '\0')
								System.out.print(s.charAt(j));
							else
								System.out.print(" ");
						}
					}
					else{
						((VarcharItem)value).value.setLength(24);
						for(int j=0; j<Constant.MAXNAME; j++){
							if(((VarcharItem)value).value.charAt(j) != '\0')
								System.out.print(((VarcharItem)value).value.charAt(j));
							else
								System.out.print(" ");
						}
					}
				}
				System.out.println();
			}
			
			query.Close();
		}
	}
}
